package com.central.player.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.central.model.common.PageResult;
import com.central.model.common.utils.HttpsUtil;
import com.central.model.common.utils.PageUtil;
import com.central.player.dao.PlayerDao;
import com.central.player.model.*;
import com.central.player.service.PlayerService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 玩家查询业务实现类
 */
@Slf4j
@Service
public class PlayerServiceImpl implements PlayerService {


    private Logger logger = LoggerFactory.getLogger(PlayerServiceImpl.class);

    @Autowired
    private PlayerDao playerDao;

    @Override
    public PageResult<PlayerBase> findPlayers(Map<String, Object> params) {
        int total = playerDao.count(params);
        params.forEach((k, v) -> System.out.println(k + ":" + v));
        PageUtil.pageParamConver(params, true, total);
        params.forEach((k, v) -> System.out.println(k + ":" + v));
        List<PlayerBase> list = Collections.emptyList();
        if (total > 0) {
            list = playerDao.findList(params);
        }
        return PageResult.<PlayerBase>builder().data(list).code(0).count((long) total).build();
    }

    @Override
    public PageResult<PlayerDetailsVO> findPlayerDetailed(Long playerId) {
        int total = 1;
        int code;
        List<PlayerDetailsDO> playerDetailsDOS = playerDao.playerDetailed(playerId);
        List<PlayerDetailsVO> playerDetailsVOS = new ArrayList<>();
        if (playerDetailsDOS.size() == 1) {
            code = 0;
            log.info(playerDetailsDOS.get(0).toString());
            playerDetailsVOS.add(new PlayerDetailsVO(playerDetailsDOS.get(0)));
        } else {
            return null;
        }
        return PageResult.<PlayerDetailsVO>builder().data(playerDetailsVOS).code(code).count((long) total).build();
    }

    @Override
    public List<Guest> listGuest() {
        return playerDao.listGuest();
    }

    @Override
    public RegisterAndLogin countLoginToday() {
        return playerDao.countLoginToday();
    }

    @Override
    public Integer countOnline() {
        HallUrl loginUrl = playerDao.getloginUrl("login");
        String url;
        url = HttpsUtil.splicingUrl(loginUrl.getIp(),loginUrl.getPort(),"/getHallOpenUrl");
        Integer onlineCount = 0;
        try {
            String loginData = HttpsUtil.post(url, new HashMap<>(), new HashMap<>(), null);
            log.info("大厅地址:" + loginData);
            JSONArray jsonArray = JSONArray.parseArray(loginData);
            jsonArray.forEach(json->{
                HallUrl hallUrl = JSON.parseObject(json.toString(),HallUrl.class);
                log.info(hallUrl.toString());
            });
            for (int i = 0; i< jsonArray.size();i++){
                HallUrl hallUrl = JSON.parseObject(jsonArray.get(i).toString(),HallUrl.class);
                url = HttpsUtil.splicingUrl(hallUrl.getIp(),hallUrl.getPort(),"/onlineCount");
                try {
                    String hallData = HttpsUtil.post(url, new HashMap<>(), new HashMap<>(), null);
                    JSONObject codeJson = JSONObject.parseObject(hallData);
                    if (codeJson.containsKey("body")){
                        onlineCount += Integer.valueOf(codeJson.get("body").toString());
                    }
                    logger.info("大厅数据" + hallUrl.getIp());
                } catch (Exception e) {
                    log.info("获取大厅在线人数失败");
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            log.info("获取大厅地址失败");
            e.printStackTrace();
        }
        return onlineCount;
    }

    @Override
    public CountRechargeDO getRecharge() {
        CountRechargeDO countRechargeDO = playerDao.countTodayRecharge();
        countRechargeDO.setTotalRechargeMoney(playerDao.countAllRecharge());
        return countRechargeDO;
    }

    public static void main(String[] args) {
        String str = "[{\"id\":2,\"ip\":\"192.168.1.106\",\"port\":6001},{\"id\":3,\"ip\":\"192.168.1.110\",\"port\":6011}]";
        JSONArray jsonArray = JSONArray.parseArray(str);
        jsonArray.forEach(json->{
            HallUrl url = JSON.parseObject(json.toString(),HallUrl.class);
            System.out.println(url);
        });

        String code = "{\"prop\":{\"0\":10},\"boxKey\":{\"0\":1}}";
        JSONObject codeJson = JSONObject.parseObject(code);
        System.out.println(codeJson.get("prop").getClass());

    }
}
